function throttle(fn, delay) {
  let last
  let timer

  delay = delay || 200

  return (...args) => {
    const now = Date.now()

    if (last && now - last < delay) {
      clearTimeout(timer)

      timer = setTimeout(() => {
        last = now
        fn.apply(this, args)
      }, delay)
    } else {
      last = now
      fn.apply(this, args)
    }
  }
}

export default throttle
